package cn.hznc.controller.service.toolConnect;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.constant.CommonConstant;
import cn.hznc.controller.service.toolfork.ToolForkService;
import cn.hznc.domain.entity.OrderEntity;
import cn.hznc.domain.entity.ToolConnectEntity;
import cn.hznc.domain.entity.ToolLifeEntity;
import cn.hznc.domain.request.common.QueryCondition;
import cn.hznc.domain.request.toolconnect.AddOrEditToolConnectReq;
import cn.hznc.domain.request.toolconnect.QryToolConnectListReq;
import cn.hznc.domain.response.PageResult;
import cn.hznc.mapper.ToolConnectMapper;
import cn.hznc.mapper.ToolLifeMapper;
import cn.hznc.repository.ToolLifeRepository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.print.Collation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/12/18 15:53
 */
@Service
public class ToolConnectService{

    private static final HashMap<String, String> fieldMap = new HashMap<>();

    static {
        fieldMap.put("toolNo" , "tool_no");
        fieldMap.put("partno" , "partno");
        fieldMap.put("partType" , "part_type");
    }

    @Resource
    private ToolConnectMapper toolConnectMapper;

    @Resource
    private ToolLifeMapper toolLifeMapper;

    @Resource
    private ToolForkService toolForkService;

    @Resource
    private ToolLifeRepository toolLifeRepository;

    public Object qryToolConnectList(QryToolConnectListReq qryToolConnectListReq) {
        PageHelper.startPage(qryToolConnectListReq.getPageNum() , qryToolConnectListReq.getPageSize());
        QueryWrapper<ToolConnectEntity> wrapper = new QueryWrapper<>();
        buildQueryWrapper(wrapper , qryToolConnectListReq.getQueryConditions());
        PageInfo<ToolConnectEntity> pageInfo = new PageInfo<>(toolConnectMapper.selectList(wrapper));
        if(CollectionUtil.isNotEmpty(pageInfo.getList())){
            for (ToolConnectEntity toolConnectEntity : pageInfo.getList()) {
                QueryWrapper<ToolLifeEntity> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("classify" , toolConnectEntity.getPartno());
                wrapper1.eq("type_flag" , 0);
                List<ToolLifeEntity> toolLifeEntity = toolLifeMapper.selectList(wrapper1);
                if(CollectionUtil.isNotEmpty(toolLifeEntity)){
                    toolConnectEntity.setLife(toolLifeEntity.get(0).getLife().toString());
                }

            }
        }
        return new PageResult<>(pageInfo.getList() , pageInfo.getTotal());
    }

    private void buildQueryWrapper(QueryWrapper<ToolConnectEntity> wrapper, List<QueryCondition> queryConditions) {
        for (QueryCondition queryCondition : queryConditions) {
            if (CommonConstant.EQUALS_CONDITION_CONST.equals(queryCondition.getCondition())) {
                wrapper.eq(queryCondition.getKey(), queryCondition.getValue());
            }
            if (CommonConstant.LIKE_CONDITION_CONST.equals(queryCondition.getCondition())) {
                wrapper.like(fieldMap.get(queryCondition.getKey()), queryCondition.getValue());
            }
            if (CommonConstant.BETWEEN_CONDITION_CONST.equals(queryCondition.getCondition())) {
                wrapper.between(fieldMap.get(queryCondition.getKey()), queryCondition.getStartValue(), queryCondition.getEndValue());
            }
            if (CommonConstant.NOT_EQUALS_CONDITION_CONST.equals(queryCondition.getCondition())) {
                wrapper.ne(fieldMap.get(queryCondition.getKey()), queryCondition.getValue());
            }
        }
    }

    public Object addOrEditToolConnect(AddOrEditToolConnectReq addOrEditToolConnectReq) {
        List<ToolConnectEntity> insertList = new ArrayList<>();
        List<ToolConnectEntity> updateList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(addOrEditToolConnectReq.getList())){
            addOrEditToolConnectReq.getList().forEach(toolConnectEntity -> {
                if(ObjectUtil.isNotNull(toolConnectEntity.getId())){
                    //修改
                    ToolConnectEntity existToolConnectEntity = toolConnectMapper.selectById(toolConnectEntity.getId());
                    if(ObjectUtil.isNotNull(existToolConnectEntity)){
                        BeanUtil.copyProperties(toolConnectEntity , existToolConnectEntity);
                        updateList.add(existToolConnectEntity);
                    }
                }else{
                    //新增
                    ToolConnectEntity insertEntity = new ToolConnectEntity();
                    BeanUtil.copyProperties(toolConnectEntity , insertEntity);
                    insertList.add(insertEntity);
                }
                //编辑寿命情况
                if(StringUtils.isNotBlank(toolConnectEntity.getLife()) && Integer.parseInt(toolConnectEntity.getLife()) > 0){
                    editToolForkLife(toolConnectEntity.getPartno() , new BigDecimal(toolConnectEntity.getLife()));
                }
            });
        }
        if(CollectionUtil.isNotEmpty(updateList)){
            toolConnectMapper.updateBatchById(updateList);
        }
        if(CollectionUtil.isNotEmpty(insertList)){
            toolConnectMapper.insertBatchSomeColumn(insertList);
        }
        return Boolean.TRUE;
    }

    public Object deleteBatch(List<Integer> ids) {
        if(CollectionUtil.isNotEmpty(ids)){
            toolConnectMapper.deleteBatchIds(ids);
        }
        return Boolean.TRUE;
    }

    public void editToolForkLife(String partno , BigDecimal life) {
        QueryWrapper<ToolLifeEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("classify" , partno);
        ToolLifeEntity toolLifeEntity = toolLifeMapper.selectOne(wrapper);
        if(ObjectUtil.isNotNull(toolLifeEntity)){
            toolLifeRepository.editLifeByPartno(partno , life);
            return;
        }
        ToolLifeEntity insertEntity = new ToolLifeEntity();
        insertEntity.setClassify(partno);
        insertEntity.setLife(life);
        insertEntity.setTypeFlag(0);
        toolLifeMapper.insert(insertEntity);
    }
}
